﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnCube : MonoBehaviour
{

    Transform Child;
    public float Speed;
    public GameObject SpawnTing;
    public DiceData dice;
    public Quaternion Quan;
    private bool isSpawned;
    private float time;

    private void OnEnable()
    {
        Child = transform.GetChild(0);
        time = 0;
        Child.localScale = new Vector3(1, 0, 1);
        transform.localScale = new Vector3(1, 1, 1);
        isSpawned = false;
    }

    void Update()
    {
        if (time < Speed)
            Child.localScale = Vector3.Lerp(new Vector3(1, 0, 1), new Vector3(1, 1, 1), time / Speed);
        if (time > Speed && time < Speed * 2)
        {
            if (!isSpawned)
            {
                if(SpawnTing != null)
                Instantiate(SpawnTing, dice.DiceCoordinate + dice.GroundTowards * 0.5f, Quan, dice.Obj.transform.GetChild(0));
                isSpawned = true;
            }
            transform.localScale = Vector3.Lerp(new Vector3(1, 1, 1), new Vector3(1, 0, 1), (time - Speed) / Speed);
        }
        if (time > Speed * 2)
            Destroy(transform.parent.gameObject);//gameObject.SetActive(false);
        time += Time.deltaTime;
    }


}
